//
//  ShellProfitViewModel.m
//  ShellWallert
//
//  Created by lvzhao on 2019/10/24.
//  Copyright © 2019 VV. All rights reserved.
//

#import "ShellProfitViewModel.h"
@interface ShellProfitViewModel()

@property (nonatomic,assign) NSInteger pageIndex;

@end
@implementation ShellProfitViewModel


/**
 Convert 今日收益
 */
- (void)getTodayProfit{
    self.profitModel = [[ShellProfitModel alloc] init];
    @weakify(self);
    [LZNetworkingManager lz_request:@"get" url:kTodayProfit params:@{} success:^(id  _Nullable responseObject) {
        if([responseObject[kResultStatus] isEqualToString:kCode]){
            @strongify(self);
            NSDictionary *dataDictionary = responseObject[kResultData];
            self.profitModel = [ShellProfitModel mj_objectWithKeyValues:dataDictionary];
            [self.reloadSubject sendNext:@(6)];
            
        }
    } failure:^(NSError * _Nullable error) {} isLoading:YES isFailTip:YES];
}


/**
 Convert 历史收益
 @param  isFirst 是否是第一次
 */
- (void)getHistoryProfit:(BOOL)isFirst{
    
    if (isFirst) {
        self.pageIndex = 1;
        [self.dataArray removeAllObjects];
    }
       
    NSString *url = [NSString stringWithFormat:@"%@/%ld",kUserProfit,(long)self.pageIndex];
    @weakify(self);
    [LZNetworkingManager  lz_request:@"get" url:url params:nil success:^(id  _Nullable responseObject) {
         @strongify(self);
        if([responseObject[kResultStatus] isEqualToString:kCode]){
            NSArray *dataArray = responseObject[kResultData];
            for (int i = 0; i < dataArray.count; i++) {
                ShellProfitModel *orderModel = [ShellProfitModel mj_objectWithKeyValues:dataArray[i]];
                [self.dataArray addObject:orderModel];
            }
                  
              //分页查询没有更多数据, 或者是当前页面  ==  后台返回的总页数
              NSInteger totalCount = [responseObject[@"total"] integerValue];
              if(self.dataArray.count == totalCount){
                  [self.reloadSubject sendNext:@(0)];
                  return;
              }
              //看是否有数据
              if(self.dataArray.count == 0){
                  [self.reloadSubject sendNext:@(-1)];
              } else {
                  self.pageIndex++;
                  [self.reloadSubject sendNext:@(1)];
              }
              } else {
                  [self.reloadSubject sendNext:@(0)];
              }
      } failure:^(NSError * _Nullable error) {} notwork:^(NSError * _Nullable error) {} isLoading:NO isFailTip:YES];
}


@end
